package com.example.springboot014blogadmin.service.impl;

import com.example.springboot014blogadmin.mapper.ArticleMapper;
import com.example.springboot014blogadmin.model.dto.ArticleDTO;
import com.example.springboot014blogadmin.model.param.ArticleParam;
import com.example.springboot014blogadmin.po.ArticleDetailPO;
import com.example.springboot014blogadmin.po.ArticlePO;
import com.example.springboot014blogadmin.service.ArticleService;
import com.example.springboot014blogadmin.settings.ArticleSettings;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/9 下午 03:22
 * @version: v1.0
 */

@RequiredArgsConstructor
@Service
public class ArticleServiceImpl implements ArticleService {
    private final ArticleMapper articleMapper;
    private final ArticleSettings articleSettings;
    @Override
    public List<ArticlePO> queryTopArticle() {
        Integer lowRead = articleSettings.getLowRead();
        Integer topRead = articleSettings.getTopRead();
        return articleMapper.topSortByReadCount(lowRead, topRead);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean addArticle(ArticleDTO articleDTO) {
        ArticlePO articlePO = new ArticlePO();
        articlePO.setTitle(articleDTO.getTitle());
        articlePO.setSummary(articleDTO.getSummary());

        articlePO.setUserId(new Random().nextInt(1000));
        articlePO.setReadCount(new Random().nextInt(500));
        articlePO.setCreateTime(LocalDateTime.now());
        articlePO.setUpdateTime(LocalDateTime.now());
        articleMapper.insertArticle(articlePO);
        ArticleDetailPO articleDetailPO = new ArticleDetailPO();
        articleDetailPO.setArticleId(articlePO.getId());
        articleDetailPO.setContent(articleDTO.getContent());
        articleMapper.insertArticleDetail(articleDetailPO);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modifyArticle(ArticleParam param) {
        ArticlePO articlePO = new ArticlePO();
        articlePO.setId(param.getId());
        articlePO.setTitle(param.getTitle());
        articlePO.setSummary(param.getSummary());
        articlePO.setUpdateTime(LocalDateTime.now());
        int editArticle = articleMapper.updateArticle(articlePO);

        ArticleDetailPO detailPO = new ArticleDetailPO();
        detailPO.setArticleId(param.getId());
        detailPO.setContent(param.getContent());
        System.out.println(detailPO);
        int editDetail = articleMapper.updateArticleDetail(detailPO);


        if (editArticle > 0 && editDetail > 0){
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int removeArticle(List<Integer> ids) {
        int master = articleMapper.deleteArticle(ids);
        int detail = articleMapper.deleteArticleDetail(ids);
        return master;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ArticleDTO queryByArticleId(Integer id) {
        return articleMapper.selectArticleAndDetail(id);
    }

    @Override
    public String queryTop20Detail(Integer id) {
        ArticleDetailPO articleDetailPO = articleMapper.selectDetailByArticleId(id);
        String content = articleDetailPO.getContent();
        if (StringUtils.hasText(content)){
            content = content.substring(0, content.length() >= 1000 ? 1000 : content.length());
        }
        return content;
    }
}
